package com.sdry.transit.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sdry.basic.entity.*;
import com.sdry.basic.service.*;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseTypeEnum;
import com.sdry.common.ContainerTypeEnum;
import com.sdry.good.logistics.entity.ReportPicking;
import com.sdry.system.entity.SysUser;
import com.sdry.system.mapper.UserMapper;
import com.sdry.transit.entity.TransitLoading;
import com.sdry.transit.entity.TransitLoadingDetail;
import com.sdry.transit.entity.TransitOrder;
import com.sdry.transit.entity.TransitOrderDetail;
import com.sdry.transit.service.TransitLoadingDetailService;
import com.sdry.transit.service.TransitLoadingService;
import com.sdry.transit.service.TransitOrderService;
import com.sdry.transit.service.TransitOrderDetailService;
import com.sdry.untils.DateUtil;
import com.sdry.untils.ResponseUtil;
import lombok.Data;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *@ClassName: TransitOrderController
 *@Description: 中转区生产指令
 *@Author tdd
 *@Date 2020-2-19 15:38:44
 *@version 1.0
*/
@Controller
@RequestMapping("/transitOrder")
public class TransitOrderController {
	@Resource
	TransitOrderService transitOrderService;
	@Resource
	TransitOrderDetailService transitOrderDetailService;
	@Resource
	ContainerService containerService;
	@Resource
    CarService carService;
    @Resource
    DriverService driverService;
    @Resource
    ContainerStareService containerStareService;
    @Resource
	TransitLoadingDetailService transitLoadingDetailService;

    @Resource
	private UserMapper userMapper;

	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(TransitOrderController.class);
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageTransitOrder (Model model) {
		return "/td/transit/transitOrder";
	}
	/**
	 * 根据条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<TransitOrder> queryAllByMution(TransitOrder param) {
		logger.info("根据条件查询所有条件"+JSONArray.fromObject(param));
		List<TransitOrder> list = transitOrderService.queryAllByMution(param);
		logger.info("根据条件查询所有返回内容"+JSONArray.fromObject(list));
		return list;
	}
	/** 
	 * 根据条件分页查询
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/transitOrderList")
	@ResponseBody
	public JSONObject transitOrderList(TransitOrder param,HttpServletResponse response){
		param.setToisdelete("0");
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<TransitOrder> list = transitOrderService.findPageByMution(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = transitOrderService.selectCountByMution(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		return jsonObject;
	}
	/**
	 * 根据条件分页查询
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/transitOrderDetailList")
	public void transitOrderDetailList(TransitOrderDetail param,HttpServletResponse response){
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<TransitOrderDetail> list = transitOrderDetailService.findPageByMution(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = transitOrderDetailService.selectCountByMution(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			logger.info("根据条件分页查询返回数据失败");
			e.printStackTrace();
		}
	}

	/**
	 * 插入生产指令返回id
	 * @param param 生产指令
	 * @param mids 物流ID
	 * @param todnums 数量
	 * @param todSprayingRequirements 喷涂要求
	 * @param todSpecialRequirements 特殊要求
	 * @param urgent 紧急程度
	 * @param response
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public Long insert(TransitOrder param,Long[] mids,int[] todnums
			,String[] todSprayingRequirements
			,String[] todSpecialRequirements
			,String[] urgent,String[] todremark,String[] todremark1,
					   HttpServletResponse response, HttpServletRequest request){
		param.setToisdelete("0");
		param.setTostare("0");
		param.setTocreateTime(DateUtil.dateFormat3());
		SysUser user = (SysUser) request.getSession().getAttribute("user");
		param.setTocreateMan(user.getUsername());
		logger.info("插入返回id插入内容"+JSONArray.fromObject(param));
		Long id = transitOrderService.insertPl(param,mids,todnums
			,todSprayingRequirements
			,todSpecialRequirements
			,urgent,todremark,todremark1);
		logger.info("插入返回id"+id);
		return id;
	}

	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	@ResponseBody
	public Integer delete(String ids,HttpServletResponse response){
		logger.info("根据主键拼接的字符串删除返回影响行数：主键拼接"+ids);
		ids=ids.replace("'",""); 
		Integer count = transitOrderService.delete(ids);
		logger.info("根据主键拼接的字符串删除返回影响行数"+count);
		return count;
	}
	/****************************************app*******************************************/
    /**
     * app查询生产指令
     * @return 实体
     */
    @RequestMapping("/api/findTransitOrder")
	@ResponseBody
    public JSONObject findTransitOrder(HttpServletResponse response) {
        TransitOrder transitOrder = new TransitOrder();
        //是否删除 0否 1是
        transitOrder.setToisdelete("0");
        //查询当天单据
        transitOrder.setTocreateTime(DateUtil.dateFormat1());
		//修改收货后不再操作
        List<TransitOrder> list = new ArrayList<>();
		transitOrder.setTostare("0");
        List<TransitOrder> list1 = transitOrderService.queryAllByMution(transitOrder);
		list.addAll(list1);
		transitOrder.setTostare("1");
		List<TransitOrder> list2 = transitOrderService.queryAllByMution(transitOrder);
		list.addAll(list2);
        JSONObject jsonObject = new JSONObject();
        if(list.size() > 0){
            jsonObject.put("status", "1");
            jsonObject.put("data", list);
        } else {
            jsonObject.put("status", "0");
            jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());

        }
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		return jsonObject;
    }

	/**
	 * app查询生产指令详情
	 * @param toid 生产指令id
	 * @param maNumber 物料编号
	 * @param response
	 * @return
	 */
    @RequestMapping("/api/findTransitOrderDetail")
    public void findTransitOrderDetail(Long toid,String maNumber,HttpServletResponse response) {
        TransitOrderDetail transitOrderDetail = new TransitOrderDetail();
        transitOrderDetail.setToid(toid);
		Material material = new Material();
		material.setMaNumber(maNumber);
        transitOrderDetail.setMaterial(material);
        List<TransitOrderDetail> list = transitOrderDetailService.queryAllByMution(transitOrderDetail);
        JSONObject jsonObject = new JSONObject();
		if(list.size() > 0) {
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    /**
     * app根据编号查询容器信息
     * @param cnumber 框码
     * @param permissionType 产线/库区编号
     * @param response
     * @return
     */
    @RequestMapping("/api/findContainerByCnumber")
    public void findContainerByCnumber(String cnumber,String permissionType,HttpServletResponse response) {
    	logger.info("框码"+cnumber+"产线/库区编号"+permissionType);
        Container container = new Container();
        container.setCnumber(cnumber);
        List<Container> list = containerService.queryAllByMution(container);
        JSONObject jsonObject = new JSONObject();
        if(list.size() == 1) {
			container = list.get(0);
			Long cid = container.getCid();
			ContainerStare containerStare = new ContainerStare();
			containerStare.setCid(cid);
			List<ContainerStare> lists = containerStareService.queryAllByMution(containerStare);
			//容器空闲或在对应位置
			 if (lists.size() == 0 || (null== lists.get(0).getWrlid() && null == ContainerTypeEnum.getNameByCode(permissionType)) || lists.get(0).getWrlid().equals(ContainerTypeEnum.getNameByCode(permissionType))){
				 //如果查询的是中转区的判断是否已装车 中转装框
				 if(lists.size() > 0 && permissionType.equals(ContainerTypeEnum.WZZ_WRLID.getCode())){
					 container.setCremark5(lists.get(0).getCsrremark2());

					 //获取改容器记录信息
					 ContainerStareRecord containerStareRecord = new ContainerStareRecord();
					 containerStareRecord.setCid(cid);
					 containerStareRecord.setCsrremark1("0");
					 List<ContainerStareRecord> containerStareRecords = containerStareService.queryContainerStareRecordByMution(containerStareRecord);
					 if(containerStareRecords.size() > 0){
						//根据记录id查装载详情
						 TransitLoadingDetail transitLoadingDetail = new TransitLoadingDetail();
						 transitLoadingDetail.setCsid(containerStareRecords.get(0).getCsrid());
						 List<TransitLoadingDetail> transitLoadingDetails = transitLoadingDetailService.queryAllByMution(transitLoadingDetail);
						 if(transitLoadingDetails.size() > 0){
							 jsonObject.put("status","0");
							 jsonObject.put("message",BaseAPIMessageTypeEnum.A025.getName());
						 }else{
							 jsonObject.put("status", "1");
							 jsonObject.put("data", container);
						 }
					 }else{
						 jsonObject.put("status","0");
						 jsonObject.put("message",BaseAPIMessageTypeEnum.A028.getName());
					 }

				 }else{
					 jsonObject.put("status", "1");
					 jsonObject.put("data", container);
				 }
			//该容器不在对应位置（不空闲且不在对应位置）
			}else {
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A019.getName());
			}
        }else if(list.size() > 1) {
            jsonObject.put("status","0");
            jsonObject.put("message",BaseAPIMessageTypeEnum.A002.getName());
        }else{
            jsonObject.put("status","0");
            jsonObject.put("message",BaseAPIMessageTypeEnum.A003.getName());
        }
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	/**
     * app装框
	 * @param json 装框信息
     * @param cnumber 框码
     * @param userId 登录人id
     * @param permissionType 产线/库区编号
     * @return 实体
     */
    @RequestMapping("/api/boxing")
	@ResponseBody
    public JSONObject boxing(String json,String cnumber,Long userId,String permissionType, HttpServletResponse response) {
		logger.info("装框信息"+json+"框码"+cnumber+"登录人id"+userId+"产线/库区编号"+permissionType);
        //获取登录人
        SysUser user = userMapper.selectById(userId);
		logger.info("登录人"+JSONArray.fromObject(user));
        JSONObject jsonObject = new JSONObject();
		//json转list集合
		Gson gson = new Gson();
		List<TransitOrderDetail> transitOrderDetails = gson.fromJson(json, new TypeToken<List<TransitOrderDetail>>() {}.getType());
		//判断提交数据是否为空
		if (null != transitOrderDetails && transitOrderDetails.size() > 0 && null != cnumber && !"".equals(cnumber)) {
			//根据容器编号查询容器
			Container container = new Container();
			container.setCnumber(cnumber);
			List<Container> containers = containerService.queryAllByMution(container);
			//判断是否根据编号查询到唯一容器
			if (null != containers && containers.size() == 1) {
				//添加容器装载记录
				Long flg = null;
				Long cid = containers.get(0).getCid();
				ContainerStare containerStare = new ContainerStare();
				containerStare.setCid(cid);
				List<ContainerStare> list = containerStareService.queryAllByMution(containerStare);
				//容器空闲或在中转区
				if (list.size() == 0 || (null== list.get(0).getWrlid() && null == ContainerTypeEnum.getNameByCode(permissionType)) || list.get(0).getWrlid().equals(ContainerTypeEnum.getNameByCode(permissionType))) {

						int isFlg = 0;
						if(list.size() > 0 && permissionType.equals(ContainerTypeEnum.WZZ_WRLID.getCode())) {
							//获取该容器记录信息
							ContainerStareRecord containerStareRecord = new ContainerStareRecord();
							containerStareRecord.setCid(cid);
							containerStareRecord.setCsrremark1("0");
							List<ContainerStareRecord> containerStareRecords = containerStareService.queryContainerStareRecordByMution(containerStareRecord);
							//根据记录id查装载详情
							TransitLoadingDetail transitLoadingDetail = new TransitLoadingDetail();
							transitLoadingDetail.setCsid(containerStareRecords.get(0).getCsrid());
							List<TransitLoadingDetail> transitLoadingDetails = transitLoadingDetailService.queryAllByMution(transitLoadingDetail);
							if (transitLoadingDetails.size() > 0) {
								isFlg = 1;
							}
						}
						if(isFlg > 0){
							jsonObject.put("status", "0");
							jsonObject.put("message", BaseAPIMessageTypeEnum.A025.getName());
						}else{
							//获取装入物料的起始位置
							List<String> todremark1List = transitOrderDetails.stream().map(TransitOrderDetail -> TransitOrderDetail.getTodremark1()).collect(Collectors.toList());
							todremark1List = todremark1List.stream().distinct().collect(Collectors.toList());
							//判断装入物料的起始位置是否一致及如果框不空闲，是否和框中物料起始位置一致
							if(todremark1List.size() == 1 && (list.size() == 0 || todremark1List.get(0).equals(list.get(0).getCsrremark2()))){
								//装框
								ContainerStareRecord containerStareRecord = new ContainerStareRecord();
								containerStareRecord.setCid(cid);
								containerStareRecord.setCsrcreateMan(user.getUsername());
								containerStareRecord.setCsrcreateTime(DateUtil.dateFormat3());
								//起始位置
								containerStareRecord.setCsrremark2(todremark1List.get(0));
								//进行遍历装框绑定
								for (int i = 0; i < transitOrderDetails.size(); i++) {
									TransitOrderDetail transitOrderDetail = transitOrderDetails.get(i);
									containerStareRecord.setMaId(transitOrderDetail.getMid());
									containerStareRecord.setCsrnum(transitOrderDetail.getTodnum());
									containerStareRecord.setCsrremark(transitOrderDetail.getTodid() + "");
									containerStareRecord.setCsrremark1("0");
									//容器装载记录插入数据返回id(判断容器是否在空闲或在添加数据位置且修改对应的容器状态表、中转区则修改对应状态)
									Long csrid = containerStareService.containerStareRecordInsert(containerStareRecord);
									if (null != csrid && csrid > 0) {
										flg = csrid;
									} else {
										flg = null;
										break;
									}
								}
								//装框成功
								if (null != flg && flg > 0) {
									jsonObject.put("status", "1");
									jsonObject.put("data", BaseAPIMessageTypeEnum.A004.getName());
									//装框失败
								} else {
									jsonObject.put("status", "0");
									jsonObject.put("message", BaseAPIMessageTypeEnum.A005.getName());
								}
							}else{
								//提示请选择同一起始位置的物料
								jsonObject.put("status", "0");
								jsonObject.put("message", BaseAPIMessageTypeEnum.A027.getName());
							}
						}

				//该容器不在对应位置（不空闲且不在中转区）
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A019.getName());
				}
			//该编号容器查出多条，请核实容器信息
			}else if(null != containers && containers.size() > 1) {
				jsonObject.put("status","0");
				jsonObject.put("message",BaseAPIMessageTypeEnum.A002.getName());
			//没有该编号容器
			}else{
				jsonObject.put("status","0");
				jsonObject.put("message",BaseAPIMessageTypeEnum.A003.getName());
			}
		} else if((null != transitOrderDetails && transitOrderDetails.size() > 0) && (null == cnumber || "".equals(cnumber))){
			//提交框码为空
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A026.getName());
		} else {
			//提交数据为空
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		//公共提交数据
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		return jsonObject;
	}


    /**
     *app查询车辆和司机信息使用的车辆和司机总和
     */
	@Data
    public class CarAndDriver{
	    /** 车辆*/
        private List<Car> cars;
        /** 司机信息*/
        private List<Driver> drivers;

    }
    /**
     * app查询车辆和司机信息
     * @param response
     * @return
     */
    @RequestMapping("/api/findCarAndDriver")
    public void findCar(HttpServletResponse response) {
        CarAndDriver carAndDriver = new CarAndDriver();

        Car car = new Car();
        List<Car> cars = carService.queryAllByMution(car);

        Driver driver = new Driver();
        List<Driver> drivers = driverService.queryAllByMution(driver);

        carAndDriver.setCars(cars);
        carAndDriver.setDrivers(drivers);
        JSONObject jsonObject = new JSONObject();
        if(cars.size() > 0 && drivers.size() > 0) {
            jsonObject.put("status", "1");
            jsonObject.put("data", carAndDriver);
        }else if(cars.size() > 0 && drivers.size() == 0) {
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A007.getName());
        }else if(cars.size() == 0 && drivers.size() > 0) {
			jsonObject.put("status","0");
			jsonObject.put("message",BaseAPIMessageTypeEnum.A008.getName());
        }else{
            jsonObject.put("status","0");
            jsonObject.put("message",BaseAPIMessageTypeEnum.A001.getName());
        }
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * app根据编号查询容器装载信息 中转区装车时使用
     * @param cnumber 框码
	 * @param permissionType 产线/库区编号
     * @param response
     * @return
     */
    @RequestMapping("/api/findContainerStareByCnumber")
    public void findContainerStareByCnumber(String cnumber,String permissionType,HttpServletResponse response) {
		logger.info("框码"+cnumber+"产线/库区编号"+permissionType);
		/*ContainerStareRecord containerStareRecord = new ContainerStareRecord();
        Container container = new Container();
        container.setCnumber(cnumber);
		containerStareRecord.setContainer(container);
		containerStareRecord.setCsrremark1("0");
        List<ContainerStareRecord> list = containerStareService.queryContainerStareRecordByMution(containerStareRecord);*/

		ContainerStare containerStare = new ContainerStare();
        Container container = new Container();
        container.setCnumber(cnumber);
		containerStare.setContainer(container);
		containerStare.setCsrremark1("0");
        List<ContainerStare> list = containerStareService.queryAllByMution(containerStare);
        JSONObject jsonObject = new JSONObject();
        if(list.size() > 0) {
        	//判断是否在对应位置
        	if((null== list.get(0).getWrlid() && null == ContainerTypeEnum.getNameByCode(permissionType)) || list.get(0).getWrlid().equals(ContainerTypeEnum.getNameByCode(permissionType))){
				//如果查询的是中转区的判断是否已装车
        		if(permissionType.equals(ContainerTypeEnum.WZZ_WRLID.getCode())){
        			//获取改容器记录信息
					ContainerStareRecord containerStareRecord = new ContainerStareRecord();
					containerStareRecord.setCid(list.get(0).getCid());
					containerStareRecord.setCsrremark1("0");
					List<ContainerStareRecord> containerStareRecords = containerStareService.queryContainerStareRecordByMution(containerStareRecord);
					if(containerStareRecords.size() > 0){
                        //根据记录id查装载详情
                        TransitLoadingDetail transitLoadingDetail = new TransitLoadingDetail();
                        transitLoadingDetail.setCsid(containerStareRecords.get(0).getCsrid());
                        List<TransitLoadingDetail> transitLoadingDetails = transitLoadingDetailService.queryAllByMution(transitLoadingDetail);
                        if(transitLoadingDetails.size() > 0){
                            jsonObject.put("status","0");
                            jsonObject.put("message",BaseAPIMessageTypeEnum.A025.getName());
                        }else{
                            jsonObject.put("status", "1");
                            jsonObject.put("data", list);
                        }
                    }else{
                        jsonObject.put("status","0");
                        jsonObject.put("message",BaseAPIMessageTypeEnum.A028.getName());
                    }

				}else{
					jsonObject.put("status", "1");
					jsonObject.put("data", list);
				}
			}else{
				jsonObject.put("status","0");
				jsonObject.put("message",BaseAPIMessageTypeEnum.A019.getName());
			}
        }else{
            jsonObject.put("status","0");
            jsonObject.put("message",BaseAPIMessageTypeEnum.A009.getName());
        }
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	/**
	 * app装车
	 * @param json 容器装载信息
	 * @param  cplate 车牌号
	 * @param dnumber 司机编号
	 * @param userId 登录人id
	 * @return 实体
	 */
	@RequestMapping("/api/addTransitLoading")
	@ResponseBody
	public JSONObject addTransitLoading(String json,String cplate,String dnumber,Long userId,HttpServletResponse response) {
        logger.info("装载信息"+json+"车牌号"+cplate+"司机编号"+dnumber+"登录人id"+userId);
		//获取登录人
        SysUser user = userMapper.selectById(userId);
		logger.info("登录人"+JSONArray.fromObject(user));
	    //json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		/*List<ContainerStareRecord> containerStareRecords = gson.fromJson(json, new TypeToken<List<ContainerStareRecord>>() {}.getType());
		//判断传入的容器装载信息是否为空
		if(null != containerStareRecords && containerStareRecords.size() > 0) {*/
		List<ContainerStare> containerStares = gson.fromJson(json, new TypeToken<List<ContainerStare>>() {}.getType());
		//判断传入的容器装载信息是否为空
		if(null != containerStares && containerStares.size() > 0) {
			List<ContainerStareRecord> containerStareRecords = new ArrayList<>();
			for (int i = 0; i < containerStares.size(); i++) {
				ContainerStareRecord containerStareRecord = new ContainerStareRecord();
				containerStareRecord.setCid(containerStares.get(i).getCid());
				containerStareRecord.setMaId(containerStares.get(i).getMaId());
				containerStareRecord.setCsrremark1("0");
				List<ContainerStareRecord> containerStareRecords1 = containerStareService.queryContainerStareRecordByMution(containerStareRecord);
				containerStareRecords.addAll(containerStareRecords1);
			}

			//根据车牌号查询车辆信息
			Car car = new Car();
			car.setCplate(cplate);
			List<Car> cars = carService.queryAllByMution(car);
			//根据编号查询司机信息
			Driver driver = new Driver();
			driver.setDnumber(dnumber);
			List<Driver> drivers = driverService.queryAllByMution(driver);
			if(cars.size() == 1 && drivers.size() == 1){
				Long tlid = transitOrderService.addTransitLoading(containerStareRecords,cars.get(0).getCid(),drivers.get(0).getDid(),user.getUsername());
				if(null != tlid && tlid > 0){
					jsonObject.put("status", "1");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A010.getName());
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A014.getName());
				}
			}else if(cars.size() > 1 || drivers.size() > 1){
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A011.getName());
			}else if(cars.size() == 0 || drivers.size() == 0){
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A012.getName());
			}

		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		return jsonObject;
	}
}
